package com.shnu.cyn.studentscore.controller;

import com.shnu.cyn.studentscore.entity.ClassEntity;
import com.shnu.cyn.studentscore.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "/sample/")
public class ClassController {
    @Autowired
    private ClassService classService;

    @RequestMapping(value = "classes/{id}", method = RequestMethod.GET)
    public String find(@PathVariable("id") String id, Model model) {
        ClassEntity dto = classService.getClassEntity(id);
        model.addAttribute("classEntity",dto);
        return "/sample/class/detail";
    }

    @RequestMapping(value = "classes", method = RequestMethod.GET)
    public String findAll( Model model) {
        List<ClassEntity> lst = classService.findAll();
        model.addAttribute("classes",lst);
        return "/sample/class/list";
    }
    @RequestMapping(value = "class", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("classEntity", new ClassEntity());
        return "/sample/class/submit";
    }


    @RequestMapping(value = "class", method = RequestMethod.POST)
    public String submitForm(@ModelAttribute ClassEntity classEntity) {
        ClassEntity dto = classService.save(classEntity);
//        ModelAttribute.addAttribute("class",dto);
        return "/sample/class/detail";
    }

}
